package com.codeavatar.catools.helper;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2022
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
public class CaToastHelper {

    private final String TAG = this.getClass().getSimpleName();
    
    private static Toast gToast = null;

    //++++++++++++++++++++++++++++++++++++++
    //++ 外部方法
    //++++++++++++++++++++++++++++++++++++++

    public static void toast(Context context,String message){
        toastFilter();
        gToast = Toast.makeText(context,message,Toast.LENGTH_LONG);
        gToast.show();
    }

    public static void toast(Context context, String ... str) {
        toastFilter();
        StringBuilder sb = new StringBuilder("");
        for (String item : str) {
            sb.append(item).append("\n");
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        gToast = Toast.makeText(context,sb.toString(),Toast.LENGTH_LONG);
        gToast.show();
    }

    public static void toast(Context context,String message,int gravity){
        toastFilter();
        gToast = Toast.makeText(context,message,Toast.LENGTH_LONG);
        gToast.setGravity((gravity==0?Gravity.CENTER:gravity),0,0);
        gToast.show();
    }

    public static void toast(Context context,View view){
        toastFilter();
        gToast = new Toast(context);
        gToast.setGravity(Gravity.BOTTOM|Gravity.CENTER,0,0);
        gToast.setView(view);
        gToast.show();
    }

    public static void toast(Context context,View view, int gravity){
        toastFilter();
        gToast = new Toast(context);
        gToast.setGravity((gravity==0?Gravity.CENTER:gravity),0,0);
        gToast.setView(view);
        gToast.show();
    }

    //++++++++++++++++++++++++++++++++++++++
    //++ 内部方法
    //++++++++++++++++++++++++++++++++++++++

    private static void toastFilter(){
        if(null != gToast){
            gToast.cancel();
            gToast = null;
        }
    }

    //++++++++++++++++++++++++++++++++++++++
    //++ 内部方法
    //++++++++++++++++++++++++++++++++++++++

    private CaToastHelper(){
        // Private constructor to prevent unwanted construction.
    }
}
